package signBean;
import java.util.*;
import java.sql.*;


public class StudentBean {
	
	public Connection con(){
		Connection con = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sign?useUnicode=true&characterEncoding=UTF-8","root","");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
		
	}
	public void change(int id,String name,String price){
		Connection con = con();

		try {
			Statement st = con.createStatement();
			st.executeUpdate(" update  book  set  bookname ='"+name+"'  ,  price = '"+price+"'  where  id  ='"+id+"'" );
 

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public boolean check(String name,String password){
		boolean flag=false;
		Connection con = con();
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from user where name='"+name+"' and password='"+password+"'");
			if(rs.next()){
				
				flag = true;
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return flag;
	}
	
	public boolean checkName(String name){
		boolean flag=false;
		Connection con = con();
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from user where name='"+name+"'");
			if(rs.next()){
				
				flag = true;
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return flag;
	}
	
	public boolean rigester(String name,String password,String age,String adress){
		boolean flag=false;
		Connection con = con();
		try {
			Statement st= con.createStatement();
			if(!st.execute("insert into user (name,password,age,adress) values ('"+name+"','"+password+"','"+age+"','"+adress+"')")){
				flag=true;
			}
		} catch (Exception e) {
			e.printStackTrace();		
		}
		finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return flag;
	}
	

	public String getClassName(String id){
		String classname="";
		Connection con = con();
		try {
			Statement st= con.createStatement();
			ResultSet rs = st.executeQuery("select classname from classname where id='"+id+"'");
			if(rs.next()){
				classname=rs.getString("classname");
			}
		} catch (Exception e) {
			e.printStackTrace();		
		}
		finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return classname;
	}
		
	public void studentInsert(String name,String choseclassid,String gender,String age,String email,String educationbackground,String graduateuniversity,String phone,String mobile,String qq,String address,String question,String home){
		Connection con = con();
		try {
			Statement st= con.createStatement();
			st.execute("insert into a_signup (name,choseclassid,gender,age,email,educationbackground,graduateuniversity,phone,mobile,qq,address,question,home,createtime) values ('"+name+"','"+choseclassid+"','"+gender+"','"+age+"','"+email+"','"+educationbackground+"','"+graduateuniversity+"','"+phone+"','"+mobile+"','"+qq+"','"+address+"','"+question+"','"+home+"',now())");
		} catch (Exception e) {
			e.printStackTrace();		
		}
		finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}	
	}
	
	public List getClasses(){
		Connection con = con();
		List list = new LinkedList();
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from classname");
			while(rs.next()){
				String classname = rs.getString("classname");
				int id = rs.getInt("id");
				StudentClassesBean scb = new StudentClassesBean(id,classname);
				list.add(scb);	
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
			return list;
	}
	public void deleteBook(int id){
		Connection con = con();

		try {
			Statement st = con.createStatement();
			st.executeUpdate("delete from book where id = '"+id+"'");

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void insert(String name,String price){
		
		Connection con = con();
		try {
			Statement st= con.createStatement();
			st.executeUpdate("insert into book (bookname,price) values ('"+name+"','"+price+"')");		
		} catch (Exception e) {
			e.printStackTrace();		
		}
		finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
	}
	

	

}
